<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\model\ForumBbs;
use app\common\model\ForumBbsComment;

/**
 * 文章逻辑
 */
class Bbs extends AdminBase
{
    
    /**
     * 文章分类编辑
     */
    public function articleCategoryEdit($data = [])
    {
        
        $validate_result = $this->validateForumBbsCategory->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateForumBbsCategory->getError()];
        }
        
        $url = url('articleCategoryList');
        
        $result = $this->modelForumBbsCategory->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '论坛帖子分类' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelForumBbsCategory->getError()];
    }
    
    /**
     * 获取文章列表
     */
    public function getArticleList($data = [], $field = 'a.*,m.nickname,c.name as category_name', $order = '')
    {


        $this->modelForumBbs->alias('a');
        if(!empty($data['tags'])){
            $where['a.tags']=['like','%,'.$data['tags'].',%'];
        }
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if(!empty($data['username'])){
            $where['m.username']=['like','%'.$data['username'].'%'];
        }
        if(!empty($data['cate_id'])){
            $where['a.cate_id']=$data['cate_id'];
        }
        if(isset($data['state']) && $data['state']==0){
            $where['a.state']=$data['state'];
        }else{
            $where['a.state']=['neq',0];
        }
        if(!empty($data['search_data'])){
            $where['a.name | a.content']=['like','%'.$data['search_data'].'%'];
        }
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $count=$this->modelForumBbs->alias('a')->join('__MEMBER__ m' ,'a.user_id = m.id')->join('__FORUM_BBS_CATEGORY__ c' ,'a.cate_id = c.id')->where($where)->count();

        $ids=$this->modelForumBbs->alias('a')->join('__MEMBER__ m' ,'a.user_id = m.id')->join('__FORUM_BBS_CATEGORY__ c' ,'a.cate_id = c.id')->where($where)->column('a.id');
        if($ids){
            $reply=$this->modelForumBbsComment->where(['bbs_id'=>['in',$ids]])->count();
        }else{
            $reply=0;
        }

        $list=$this->modelForumBbs->alias('a')->field($field)->join('__MEMBER__ m' ,'a.user_id = m.id')->join('__FORUM_BBS_CATEGORY__ c' ,'a.cate_id = c.id')->where($where)->paginate(50);

        return ['list'=>$list,'count'=>$count,'reply'=>$reply];
    }

    /**
     * 获取文章列表
     */
    public function getCommentList($data = [], $field = 'a.*,m.nickname', $order = '')
    {



        if(!empty($data['tags'])){
            $where['a.tags']=['like','%,'.$data['tags'].',%'];
        }
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if(!empty($data['username'])){
            $where['m.username']=$data['username'];
        }
        if(!empty($data['bbs_id'])){
            $where['a.bbs_id']=$data['bbs_id'];
        }

        if(!empty($data['search_data'])){
            $where['a.content']=['like','%'.$data['search_data'].'%'];
        }
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];







        $list=$this->modelForumBbsComment->alias('a')->field($field)->join('__MEMBER__ m' ,'a.user_id = m.id')->where($where)->paginate(50);
        $count=$this->modelForumBbsComment->alias('a')->join('__MEMBER__ m' ,'a.user_id = m.id')->where($where)->count();

        return ['list'=>$list,'count'=>$count];
    }



    /**
     * 获取文章列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * 文章信息编辑
     */
    public function articleEdit($data = [])
    {

        $validate_result = $this->validateForumBbs->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateForumBbs->getError()];
        }
        
        $url = url('articleList');
       if(empty($data['state'])){
           $data['state']=2;
       }
       // empty($data['id']) && $data['member_id'] = MEMBER_ID;
        
//      $data['content'] = htmlspecialchars_decode($data['content']);
        preg_match_all('/<img.*?src="(.*?)".*?\/?>/i', $data['content'], $out, PREG_PATTERN_ORDER);      //正则匹配img标签的src属性，返回二维数组
        if (!empty($out[1])) {
            foreach ($out[1] as $v) {
                $url1 = DMOAIN_URL.$v;
                $data['content'] = str_replace($v, $url1, $data['content']);   //替换相对路径为绝对路径
            }
        }
        $result = $this->modelForumBbs->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '帖子' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '帖子操作成功', $url] : [RESULT_ERROR, $this->modelForumBbs->getError()];
    }

    /**
     * 获取文章信息
     */
    public function getArticleInfo($where = [], $field = 'a.*,m.nickname,c.name as category_name')
    {
        
        $this->modelForumBbs->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.user_id = m.id'],
                    [SYS_DB_PREFIX . 'forum_bbs_category c', 'a.cate_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelForumBbs->join = $join;
        
        return $this->modelForumBbs->getInfo($where, $field);
    }
    
    /**
     * 获取分类信息
     */
    public function getArticleCategoryInfo($where = [], $field = true)
    {
        
        return $this->modelForumBbsCategory->getInfo($where, $field);
    }
    
    /**
     * 获取文章分类列表
     */
    public function getArticleCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelForumBbsCategory->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 文章分类删除
     */
    public function articleCategoryDel($where = [])
    {
        
        $result = $this->modelForumBbsCategory->deleteInfo($where);
        
        $result && action_log('删除', '帖子分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '帖子分类删除成功'] : [RESULT_ERROR, $this->modelForumBbsCategory->getError()];
    }
    
    /**
     * 文章删除
     */
    public function articleDel($where = [])
    {
        
        $result = $this->modelForumBbs->deleteInfo($where);
        
        $result && action_log('删除', '帖子删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '帖子删除成功'] : [RESULT_ERROR, $this->modelForumBbs->getError()];
    }

    /**
     * 审核帖子
     */
    public function update_info($where = [])
    {

        if(isset($where['ids']) && is_array($where['ids'])){
            $result= ForumBbs::where(['id'=>['in',$where['ids']]])->update([
                'state'=>$where['state'],
                'update_time'=>time()
            ]);
        }else{
            $result = $this->modelForumBbs->setInfo($where);
        }




        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }


    /**
     * 回帖隐藏显示
     */
    public function comment_update_info($where = [])
    {

        if(isset($where['ids']) && is_array($where['ids'])){
            $result= ForumBbsComment::where(['id'=>['in',$where['ids']]])->update([
                'is_hide'=>$where['is_hide'],
                'update_time'=>time()
            ]);
        }else{
            $result = $this->modelForumBbsComment->setInfo($where);
        }




        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }


    /**
     * 文章分类编辑
     */
    public function articleLevelEdit($data = [])
    {

        $validate_result = $this->validateForumBbsLevel->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateForumBbsLevel->getError()];
        }

        $url = url('articleLevelList');

        $result = $this->modelForumBbsLevel->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '论坛帖子级别' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelForumBbsLevel->getError()];
    }


    /**
     * 获取分类信息
     */
    public function getArticleLevelInfo($where = [], $field = true)
    {

        return $this->modelForumBbsLevel->getInfo($where, $field);
    }

    /**
     * 获取文章分类列表
     */
    public function getArticleLevelList($where = [], $field = true, $order = '', $paginate = 0)
    {

        return $this->modelForumBbsLevel->getList($where, $field, $order, $paginate);
    }

    /**
     * 文章分类删除
     */
    public function articleLevelDel($where = [])
    {

        $result = $this->modelForumBbsLevel->deleteInfo($where);

        $result && action_log('删除', '帖子级别删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '帖子级别删除成功'] : [RESULT_ERROR, $this->modelForumBbsLevel->getError()];
    }
}
